package cloudPayAdmin.admin.controller.traffic;

import java.util.List;
import java.util.Map;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.project.m.api.common.biz.resp.BizResp;
import cloudPayAdmin.admin.dbapp.entity.traffic.TVideo;
import cloudPayAdmin.admin.service.traffic.VideoService;
import cloudPayAdmin.constant.BusiError;
import cloudPayAdmin.util.AdminLteDataTableResp;
import cloudPayAdmin.util.AdminLteUtil;
import cloudPayAdmin.util.AliyunUtil;
import cloudPayAdmin.util.BeanHelper;
import cloudPayAdmin.util.TimeUtil;
import cloudPayAdmin.util.WebUtil;
import cloudPayAdmin.util.pagebean.HqlPageBeanHelper;
import cloudPayAdmin.util.pagebean.HqlParam;
import cloudPayAdmin.util.pagebean.PageBean;

@Controller
@RequestMapping("/traffic/video")
public class VideoController {
	
	@Autowired
	HqlPageBeanHelper hqlPageBeanHelper;

	@PersistenceContext(unitName = "entityManagerFactory")
	EntityManager entityManager;
	
	@Autowired
	Environment env;
	
	@Autowired
	VideoService videoService;
	
	@RequestMapping(value = "/toListPage")
	public String toListPage(HttpServletRequest request, Map<String, Object> model) {
		return "traffic/video/video-list";
	}
	
	@RequestMapping("/getVideoListData")
	@ResponseBody
	public String getVideoListData(HttpServletRequest request) {
		Map<String, Object> paramMap = WebUtil.getSearchParam(request);
		paramMap = WebUtil.formatDateRange(paramMap, TimeUtil.DATE_PATTERN_NOSEPARTOR);
		// 需要映射的实体类
		HqlParam hqlParam = WebUtil.buildHqlParam(paramMap, TVideo.class, request);
		PageBean<TVideo> cloudpayCardPage = hqlPageBeanHelper.findHqlPageBean(entityManager, hqlParam);
		
		for(TVideo video : cloudpayCardPage.getContent()) {
			if(StringUtils.isNotBlank(video.getUrl())) {
				video.setUrl(AliyunUtil.getPublicUrl(AliyunUtil.PUBLIC_BUCKETNAME, video.getUrl(), Boolean.FALSE));
			}
			
		}
		
		AdminLteDataTableResp<TVideo> resp = AdminLteUtil.pageBeanToAdminLteDataTableResp(cloudpayCardPage,request);
		return resp.toAdminLteJsonString(TVideo.class);
	}
	
	/**
	 * 去编辑视频页面
	 * @param request
	 * @param cardId
	 * @return
	 */
	@RequestMapping("/toVideoEditPage")
	public String toVideoEditPage(HttpServletRequest request,Long videoId) {
		TVideo video = new TVideo();
		if(videoId != null) {
			TVideo video2 = videoService.findById(videoId);
			if(video2 != null) {
				video = video2;
			}
		}

		request.setAttribute("video", video);

		return "traffic/video/video-edit";
	}
	
	/**
	 * 编辑/保存视频信息
	 * @param request
	 * @param videoId
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping("/editVideo")
	public String editVideo(HttpServletRequest request,TVideo video) throws Exception {
		
		if(video.getVideoId() == null) {  //新增
			video.setCreateTime(TimeUtil.getCurrTime(TimeUtil.DATE_PATTERN_NOSEPARTOR));
			video.setUpdateTime(TimeUtil.getCurrTime(TimeUtil.DATE_PATTERN_NOSEPARTOR));
			BeanHelper.beanAttributeValueTrim(video);
			videoService.save(video);
		} else {   //更新
			TVideo newVideo = videoService.findById(video.getVideoId());
			newVideo.setUpdateTime(TimeUtil.getCurrTime(TimeUtil.DATE_PATTERN_NOSEPARTOR));
			newVideo.setVideoName(video.getVideoName());
			newVideo.setTag(video.getTag());
			videoService.save(newVideo);
		}
			
		return "redirect:/traffic/video/toListPage";
	}
	
	@RequestMapping("/toVideoUploadPage")
	public String toVideoUploadPage(HttpServletRequest request) {
		return "/traffic/video/video-upload";
	}
	
	@RequestMapping("/saveVideoUrl")
	@ResponseBody
	public String saveVideoUrl(String key,Long videoId) {
		BizResp resp = new BizResp();
		TVideo video = videoService.findById(videoId);
		if(video != null) {
			video.setUrl(key);
			video.setUpdateTime(TimeUtil.getCurrTime(TimeUtil.DATE_PATTERN_NOSEPARTOR));
			videoService.save(video);
		}
		resp.setResult_code(BusiError.ERR_CODE_SUCCESS);
		return JSON.toJSONString(resp);
	}
	
	@RequestMapping("/toVideoPlayPage")
	public String toVideoPlayPage(HttpServletRequest request,String videoUrl) {
		
		request.setAttribute("videoUrl", videoUrl);
		
		return "traffic/video/video-play";
	}
	
}
